<?php

/**
* Файл никак не используется микроном
*/

    class Enum {
        const NAME       = 'aaaa';
        const SOME_VALUE = 'bbbb';
    }

    echo Enum::NAME;

    eval(createenum('Gender', array(1 => 'Male', 0 => 'Female')));
    $g = Gender::Male();
    echo $g;

function createenum($base_class, array $args)
{
    $class_parts = preg_split('/\s+/', $base_class);
    $base_class_name = array_shift($class_parts);
    $enums = array();

    foreach ($args as $k => $enum)
    {
        $static_method = 'public static function ' . $enum .
            '() { return ' . $enum . '::instance(); }';
        $enums[$static_method] = '
            class ' . $enum . ' extends ' . $base_class_name . '{
                private static $instance = null;
                protected $value = "' . addcslashes($k, '\\') . '";
                private function __construct() {}
                private function __clone() {}
                public static function instance() {
                    if (self::$instance === null) { self::$instance = new self(); }
                    return self::$instance;
                }
            }';
    }

    $base_class_declaration = sprintf('
        abstract class %s {
            protected $value = null;
            %s
            public static function iterator() { return %sIterator::instance(); }
            public function value() { return $this->value; }
            public function __toString() { return (string) $this->value; }
        };',
        $base_class,
        implode(PHP_EOL, array_keys($enums)),
        $base_class_name);

    $iterator_declaration = sprintf('
        class %sIterator implements Iterator {
            private static $instance = null;
            private $values = array(\'%s\');
            private function __construct() {}
            private function __clone() {}
            public static function instance() {
                if (self::$instance === null) { self::$instance = new self(); }
                return self::$instance;
            }
            public function current() {
                $value = current($this->values);
                if ($value === false) { return false; }
                return call_user_func(array(\'%s\', $value));
            }
            public function key() { return key($this->values); }
            public function next() {
                next($this->values);
                return $this->current();
            }
            public function rewind() { return reset($this->values); }
            public function valid() { return (bool) $this->current(); }
        };',
        $base_class_name,
        implode('\',\'', $args),
        $base_class_name);

        return $base_class_declaration.
         "\r\n\r\n".
         $iterator_declaration.
         "\r\n\r\n".
         implode(PHP_EOL, $enums);

        
}

?>